home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / fflush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.4 KB  |  78 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <ansi.h>
  7.  
  8. #define _NFILE NFILES
  9.  
  10. #ifdef __STDC__
  11. static int _fflush(FILE *);
  12. #else
  13. static int _fflush();
  14. #endif
  15.  
  16. int fflush(fp)
  17. register FILE *fp;
  18. /*
  19.  *    implementation note:  This function has the side effect of
  20.  *    re-aligning the virtual file pointer (in the buffer) with
  21.  *    the actual file pointer (in the file) and is therefore used
  22.  *    in other functions to accomplish this re-sync operation.
  23.  */
  24. {
  25.     register int i;
  26.     
  27.     if(fp)
  28.     return(_fflush(fp));
  29.     else
  30.     {
  31.     for(i=0; i<_NFILE; ++i)
  32.     {
  33.         if(_iob[i]._flag & (_IOREAD | _IOWRT | _IORW))
  34.         _fflush(&_iob[i]);
  35.     }
  36.     return(0);
  37.     }
  38. }
  39.  
  40. static int _fflush(fp)
  41. register FILE *fp;
  42. {
  43.     register int  rv = 0;
  44.     register unsigned int f;
  45.     register long offset;
  46.     
  47.     if(fp == NULL)
  48.     return(0);
  49.     f = fp->_flag;
  50.     if (!(f & (_IORW | _IOREAD | _IOWRT)))  /* file not open */
  51.     return(0);
  52.     if(fp->_cnt > 0)             /* data in the buffer */
  53.     {
  54.     if(f & _IOWRT)                /* writing */
  55.     {
  56.         if(lwrite(fp->_file, fp->_base, (long)fp->_cnt)
  57.            != fp->_cnt) 
  58.         {
  59.         fp->_flag |= _IOERR;
  60.         rv = EOF;
  61.         }
  62.     }
  63.     else if(f & _IOREAD)             /* reading */
  64.     {
  65.         if((offset = -(fp->_cnt)) != 0L)
  66.         if(lseek(fp->_file, offset, 1) < 0)
  67.             rv = EOF;
  68.             else
  69.             fp->_flag &= ~_IOEOF;
  70.     }
  71.     }
  72.     if(f & _IORW)
  73.     fp->_flag &= ~(_IOREAD | _IOWRT);
  74.     fp->_ptr = fp->_base;
  75.     fp->_cnt = 0;
  76.     return(rv);
  77. }
  78.